@extends('admin.base')
@section('content')
    <div class="layui-card">
        <div class="layui-card-header layuiadmin-card-header-auto">
            <div class="layui-btn-group">
                <button class="batch-fun layui-btn layui-btn-sm layui-btn-danger" data-state="0">禁 用</button>
                <button class="batch-fun layui-btn layui-btn-sm layui-btn-danger" data-state="1">启用</button>
                <button class="layui-btn layui-btn-sm" id="create">添 加</button>
            </div>
        </div>
        <div class="layui-card-body">
            <table id="dataTable" lay-filter="dataTable"></table>
            <script type="text/html" id="switchTpl">
                    @{{# if(d.state == 1){ }}
                        <input type="checkbox" name="switch" value="@{{d.id}}" lay-filter="state" checked lay-skin="switch" lay-text="启用|禁止">
                    @{{# }else{ }}
                        <input type="checkbox" value="@{{d.id}}" name="switch" lay-filter="state" lay-skin="switch" lay-text="启用|禁止">
                    @{{# } }}
            </script>
            <script type="text/html" id="options">
                <div class="layui-btn-group">
                    <a class="layui-btn layui-btn-sm" lay-event="edit">编辑</a>
                    <!--<a class="layui-btn layui-btn-sm" lay-event="password">修改密码</a>
                    <a class="layui-btn layui-btn-danger layui-btn-sm " lay-event="del">删除</a>-->
                </div>
            </script>
        </div>
    </div>
@endsection
@section('script')
    <script type="text/javascript">
        layui.use(['layer','table','form','laytpl'],function () {
            var $ = layui.jquery;
            var layer = layui.layer;
            var form = layui.form;
            var table = layui.table;
            var laytpl = layui.laytpl;

            //用户表格初始化
            var dataTable = table.render({
                elem: '#dataTable',
                height: 500,
                url: "/admin/api/systems", //数据接口
                page: true, //开启分页
                id:'d',
                cols: [[ //表头
                    {checkbox: true,fixed: true},
                    {field: 'id', title: 'ID', sort: true,width:80},
                    {field: 'name', title: '登录帐号'},
                    {field: 'real_name', title: '用户姓名'},
                    {field: 'role_name', title: '所属角色'},
                    {field: 'state',width:180, title: '状态',templet: '#switchTpl'},
                    {field: 'updated_at', title: '更新时间'},
                    {fixed: 'right', width: 320,title:'操作', align:'center', toolbar: '#options'}
                ]]
            });

            //监听工具条
            table.on('tool(dataTable)', function(obj){ //注：tool是工具条事件名，dataTable是table原始容器的属性 lay-filter="对应的值"
                var data = obj.data, //获得当前行数据
                    layEvent = obj.event; //获得 lay-event 对应的值
                if (layEvent === 'del') {
                    layer.confirm('确认删除吗？', function(index){
                        $.post("",{_method:'delete',ids:[data.id]},function (result) {
                            if (result.code === 0){
                                obj.del(); //删除对应行（tr）的DOM结构
                            }
                            layer.close(index);
                            layer.msg(result.message,{icon:6})
                        });
                    });
                } else if(layEvent === 'edit'){
                    layer.open({
                        type:2,
                        title:'修改用户',
                        area:['600px','400px'],
                        content:'/admin/view/system/'+data.id+'/edit'
                    });
               }
            });

            form.on('switch(state)', function(data){
                if(data.elem.checked === true){
                    val = 1;
                }else{
                    val = 0;
                }
                $.ajax({
                    type:"POST",
                    url:"/admin/api/system/"+data.value+"/state/"+val,
                    data:{_method:"PUT"},
                    dataType:"json",
                    success: function(response){
                        //console.log(data);
                        if (response.code === 0){
                            layer.msg(response.message,{icon:1});
                        }else{
                            layer.msg(response.message,{icon:6});
                        }
                    },
                    error: function () {
                        try {

                        } catch(e) {}
                    }
                });
                return false;
            });

            //按钮批量禁用
            $(".batch-fun").click(function () {
                var state = $(this).attr('data-state');
                batch('system', state, table, dataTable);
            });

            $('#create').click(function () {
                layer.open({
                    type:2,
                    title:'添加用户',
                    area:['600px','400px'],
                    content:'/admin/view/system/store'
                });
            });
        })

    </script>
@endsection



